package com.ljx.funchat.chat.service.impl;

import com.ljx.funchat.chat.dao.RoomDao;
import com.ljx.funchat.chat.dao.RoomFriendDao;
import com.ljx.funchat.chat.dao.RoomGroupDao;
import com.ljx.funchat.chat.dao.RoomGroupMemberDao;
import com.ljx.funchat.chat.domain.entity.Room;
import com.ljx.funchat.chat.domain.entity.RoomFriend;
import com.ljx.funchat.chat.domain.entity.RoomGroup;
import com.ljx.funchat.chat.domain.entity.RoomGroupMember;
import com.ljx.funchat.chat.domain.enums.RoomStatusEnum;
import com.ljx.funchat.chat.domain.enums.RoomTypeEnum;
import com.ljx.funchat.chat.domain.vo.req.GroupMemberPageReq;
import com.ljx.funchat.chat.domain.vo.resp.ChatRoomResp;
import com.ljx.funchat.chat.domain.vo.resp.GroupMemberPageResp;
import com.ljx.funchat.chat.service.IRoomService;
import com.ljx.funchat.chat.service.adapter.RoomAdapter;
import com.ljx.funchat.common.domain.vo.req.CursorPageBaseReq;
import com.ljx.funchat.common.domain.vo.resp.CursorPageBaseResp;
import com.ljx.funchat.common.utils.AssertUtil;
import com.ljx.funchat.user.domain.entity.User;
import com.ljx.funchat.user.service.cache.UserInfoCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class RoomServiceImpl implements IRoomService {

    @Autowired
    private RoomDao roomDao;

    @Autowired
    private RoomFriendDao roomFriendDao;

    @Autowired
    private RoomGroupMemberDao roomGroupMemberDao;

    @Autowired
    private RoomGroupDao roomGroupDao;

    @Autowired
    private UserInfoCache userInfoCache;

    @Override
    public RoomFriend createFriendRoom(List<Long> ids, Integer type) {
        AssertUtil.isNotEmpty(ids, "房间创建失败，好友数量不对");
        AssertUtil.equal(ids.size(), 2, "房间创建失败，好友数量不对");
        // 查询房间是否已经存在，若存在且禁用就直接恢复
        String key = getRoomFriendKey(ids);
        RoomFriend roomFriend = roomFriendDao.getByKey(key);
        if (Objects.nonNull(roomFriend)){
            if (roomFriend.getStatus().equals(RoomStatusEnum.ERROR.getStatus())){
                roomFriendDao.restoreRoom(roomFriend.getId());
            }
        } else {
            // 创建房间并保存
            Room room = RoomAdapter.buildRoomSave(type);
            roomDao.save(room);
            roomFriend = RoomAdapter.buildRoomFriendSave(ids, room.getId());
            roomFriendDao.save(roomFriend);
        }
        return roomFriend;
    }

    private String getRoomFriendKey(List<Long> ids) {
        List<Long> res = ids.stream().sorted().collect(Collectors.toList());
        return res.get(0) + "_" + ids.get(1);
    }

    @Override
    public CursorPageBaseResp<GroupMemberPageResp> getMemberPage(GroupMemberPageReq req) {
        RoomGroup roomGroup = roomGroupDao.getByRoomId(req.getRoomId());
        CursorPageBaseResp<RoomGroupMember> resp = roomGroupMemberDao.getMemberPage(req, roomGroup);
        List<Long> userList = resp.getList().stream().map(RoomGroupMember::getUid).collect(Collectors.toList());
        Map<Long, User> userMap = userInfoCache.getBatch(userList);
        List<GroupMemberPageResp> resList = RoomAdapter.buildRoomMemberPagResp(userMap, resp.getList());
        return CursorPageBaseResp.init(resp, resList);
    }

    @Override
    public void disableRoomFriendByKey(String key) {
        roomFriendDao.disableRoomFriend(key);
    }
}
